<?php
if( !defined('IN_INDEX') )
{
	die('This page is hidden.');
}
function redirect($url)
{
?>
	<script type="text/javascript">
		{
		window.location = "<?php echo $url?>";
		}
	</script>
<?php 
}
if(!checkSession())
{
			
			redirect("index.php?action=profile&sub=edit");
	exit;
}
include('inc/functions_award.php');
include('inc/functions_blog.php');

$dateformat_long = $CONFIG['date_format_long'];

?>
<link rel="stylesheet" href="./jscript/generalstyle.css" type="text/css" media="screen" charset="utf-8" />

<div id='postpath'>
	<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
    &gt; <a href="index.php?action=blogs" title='<?php echo translate('showallblogs'); ?>'><?php echo translate('blog'); ?></a> 
    &gt; <a href="index.php?action=profile&id=<?php echo $USER->getId(); ?>"><?php echo ucf($USER->getFirstname()) . " " . ucf($USER->getLastname()); ?></a>
	&gt; <a href="index.php?action=profile&sub=edit" title='<?php echo translate('edit'); ?>'><?php echo translate('edit'); ?></a> 
	&gt; <a href="index.php?action=profile&sub=edit_admin" title='<?php echo translate('aminsettings'); ?>'><?php echo translate('adminsettings'); ?></a> &gt; <a href="index.php?action=profile&sub=new_award" title='<?php echo translate('newawardheader'); ?>'><?php echo translate('newawardheader'); ?></a>
</div>
<br />
<h1><?php echo translate('newawardheader'); ?></h1>
<?php
	if(isset($_POST['award-submit'])){
		$awradData['name'] = $_POST['award-name'];
		$awradData['gender'] = $_POST['gender'];
		$awradData['category'] = $_POST['category'];
		$awradData['requirement'] = $_POST['award-requirement'];
		$awradData['exercise'] = $_POST['exercise'];
		$awradData['level'] = $_POST['level'];
		$awradData['point'] = $_POST['award-point'];	
		$awradData['image'] = "";
		$imgName = $_FILES['award-image']['name'];
		$imgSize = $_FILES['award-image']['size'];
		$imgTmp_name = $_FILES['award-image']['tmp_name'];
		
		if(substr($imgName, strpos($imgName, '.') + 1) == "JPG" || substr($imgName, strpos($imgName, '.') + 1) == "jpg" || substr($imgName, strpos($imgName, '.') + 1) == "jpeg" || substr($imgName, strpos($imgName, '.') + 1) == "JPEG" || substr($imgName, strpos($imgName, '.') + 1) == "png" || substr($imgName, strpos($imgName, '.') + 1) == "PNG")
		{
			// Start upload process
			$saving_name = time()."_$imgName";
			$location = "./img/award_pic/$saving_name";
			// Move picture to folder
			move_uploaded_file($imgTmp_name, $location);
			$awradData['image'] = $saving_name;
		}
		
		insertAward($awradData);
		redirect("index.php?action=profile&sub=stats_awards");
		//header("Location: ".getBaseUrl()."index.php?action=profile&sub=stats_awards"); 
	}
?>

<form action="" method="POST" name="new_award" enctype="multipart/form-data">
	<div class="new-award-wrapper">
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""><?php echo translate('nameofachievement');?>:</label>
			</div>
			<div class="new-award-item-right">
				<input type="text" value="" name="award-name" />
			</div>
			<div class="clear"></div>
		</div>
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""><?php echo translate('gender');?>:</label>
			</div>
			<div class="new-award-item-right">
				<select class="select" id="gender" name="gender">
					<option value="male"><?php echo translate('male'); ?></option>
					<option value="female"><?php echo translate('female');?></option>
            	</select>
			</div>
			<div class="clear"></div>
		</div>
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""><?php echo translate('categories');?>:</label>
			</div>
			<div class="new-award-item-right">
				<select class="select" id="category" name="category">
					<option value="volume"><?php echo translate('volume'); ?></option>
					<option value="weight"><?php echo translate('weight'); ?></option>
					<option value="numberofconnections"><?php echo translate('numberofconnections'); ?></option>
					<option value="halloffameplacment"><?php echo translate('halloffameplacment'); ?></option>
					<option value="numberoffollowers"><?php echo translate('numberoffollowers'); ?></option>
					<option value="totalnumberofpersonalmessagessent"><?php echo translate('totalnumberofpersonalmessagessent'); ?></option>
					<option value="meanweightliftedinperiod"><?php echo translate('meanweightliftedinperiod'); ?></option>
					<option value="numberofexercisestrained"><?php echo translate('numberofexercisestrained'); ?></option>
					<option value="effectiveness"><?php echo translate('effectiveness'); ?></option>
					<option value="wilks"><?php echo translate('wilks'); ?></option>
            	</select>
			</div>
			<div class="clear"></div>
		</div>
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""><?php echo translate('requirement');?>:</label>
			</div>
			<div class="new-award-item-right">
				<input type="text" value="" name="award-requirement" />
			</div>
			<div class="clear"></div>
		</div>
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""><?php echo translate('exercise');?>:</label>
			</div>
			<div class="new-award-item-right">
				<select name="exercise">
				<?php
				$sql= mysql_query("SELECT lifts.exercise_id, lifts.training_id, training.training_id, exercise_name.ex_id, exercise_name.ex_name FROM lifts, training, exercise_name WHERE lifts.training_id=training.training_id AND exercise_name.ex_id=lifts.exercise_id GROUP BY lifts.exercise_id ORDER BY ex_name");
				while($row = mysql_fetch_assoc($sql))
				{	
					$ex_id = $row['exercise_id'];
					$ex_name = getExercisename($row['exercise_id']);	
					echo '<option name="ex_id" value='. $ex_id.'';
					if(isset($_POST['select']) && $_POST['select_exercise']== $ex_id) { echo " selected='selected'"; }
					echo '>'. translate($ex_name) . '</option>';
				}
				?>
				</select>
			</div>
			<div class="clear"></div>
		</div>
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""><?php echo translate('level')?>:</label>
			</div>
			<div class="new-award-item-right">
				<select class="select" id="category" name="level">
					<option value="novice"><?php echo translate('novice')?></option>
					<option value="intermediate"><?php echo translate('intermediate')?></option>
					<option value="advanced"><?php echo translate('advanced')?></option>
					<option value="expert"><?php echo translate('expert')?></option>
					<option value="elite"><?php echo translate('elite')?></option>
					<option value="worldclass"><?php echo translate('worldclass')?></option>
            	</select>
			</div>
		<div class="clear"></div>
		</div>
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""><?php echo translate('points')?>:</label>
			</div>
			<div class="new-award-item-right">
				<input type="text" value="" name="award-point" />
			</div>
			<div class="clear"></div>
		</div>
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""><?php echo translate('uploadpic')?>:</label>
			</div>
			<div class="new-award-item-right">
				<input type="file" name="award-image" />
			</div>
			<div class="clear"></div>
		</div>
		<div class="new-award-item">
			<div class="new-award-item-left">
				<label for=""></label>
			</div>
			<div class="new-award-item-right">
				<input type="submit" value="<?php echo translate('save');?>" name="award-submit" />
			</div>
			<div class="clear"></div>
		</div>
	</div>
</form>
